//
//  CoursesHandler.m
//  mUBB
//
//  Created by Cosmin Stirbu on 1/17/13.
//  Copyright (c) 2013 Cosmin Stirbu. All rights reserved.
//

#import "CoursesHandler.h"
#import "CoursesProtocol.h"
#import "Course.h"
#import "ListWrapper.h"


@implementation CoursesHandler

- (void)handleSuccess:(WSResponse *)response {
    ListWrapper *coursesList = [ListWrapper new];
    NSDictionary *coursesDictionary = [response.data objectFromJSONData];
    NSArray *jsonCourses = [coursesDictionary getJSONObjectForKey:ITEMS_TAG];
    
    for (NSDictionary *courseDictionary in jsonCourses) {
        Course *course = [Course new];
        course.courseCode = [courseDictionary getJSONObjectForKey:COURSE_CODE_TAG];
        course.courseName = [courseDictionary getJSONObjectForKey:COURSE_NAME_TAG];
        course.selfUrl = [courseDictionary getJSONObjectForKey:SELF_URL_TAG];
        [coursesList.itemsList addObject:course];
    }
    coursesList.totalNumberOfItems = [[coursesDictionary getJSONObjectForKey:TOTAL_NUMBER_OF_ITEMS_TAG] integerValue] ;
    [self.delegate didReceiveCoursesWithSuccess:coursesList forTicket:self.ticket];
}

- (void)handleError:(WSError *)error {
    if (error.errorCode == StatusCodeNotFound) {
        [self.delegate didReceiveCoursesWithSuccess:nil forTicket:self.ticket];
    } else {
        [self.delegate didReceiveCoursesWithFailure:error forTicket:self.ticket];
    }
}

@end
